In the article below, we will (i) automatically find the Option (of choice) closest to At The Money (ATM) and (ii) calculate its Implied Volatility. We focus below on Future (Monthly) Options on the Index .STOXX50E (EURO STOXX 50 EUR PRICE INDEX) ('EUREX') and .SPX (S&P 500 INDEX), although you can apply the logic below for another index. To find the ATM instrument, we simply and efficiently use the Search API. Usually, the calculation of the Black-Scholes-Merton model's Implied Volatility involves numerical techniques, since it is not a closed equation (unless restricting assumptions that log returns follow a standard normal distribution with mean is zero, $\mu$ = 0, and standard deviation is zero, $\sigma$ = 1, are made). If we used these techniques in calculating each Implied Volatility value on our computer, it would take several seconds - if not minutes - for each data point computed. I have chosen to use the Instrument Pricing Analytics (IPA) service in the Refinitiv Data Platform API Family instead, as this service allows me to send model specifications (and variables) and receive several (up to 100) computed Implied Volatility values in one go - in a few seconds. Not only does this save a great deal of time, but also many lines of code!
import refinitiv.data as rd # This is LSEG's Data and Analytics' API wrapper, called the Refinitiv Data Library for Python.
from refinitiv.data.content import historical_pricing # We will use this Python Class in `rd` to show the Implied Volatility data already available before our work.
from refinitiv.data.content import search # We will use this Python Class in `rd` to fid the instrument we are after, closest to At The Money.
import numpy as np # We need `numpy` for mathematical and array manipilations.
import pandas as pd # We need `pandas` for datafame and array manipilations.
import calendar # We use `calendar` to identify holidays and maturity dates of intruments of interest.
import pytz # We use `pytz` to manipulate time values aiding `calendar` library.
import pandas_market_calendars as mcal # Used to identify holidays. See `https://github.com/rsheftel/pandas_market_calendars/blob/master/examples/usage.ipynb` for info on this market calendar library
from datetime import datetime, timedelta, timezone # We use these to manipulate time values
from dateutil.relativedelta import relativedelta # We use `relativedelta` to manipulate time values aiding `calendar` library.
# `plotly` is a library used to render interactive graphs:
from plotly.subplots import make_subplots
import plotly.graph_objects as go
import plotly.express as px # This is just to see the implied vol graph when that field is available
import matplotlib.pyplot as plt # We use `matplotlib` to just in case users do not have an environment suited to `plotly`.
from IPython.display import clear_output # We use `clear_output` for users who wish to loop graph production on a regular basis.
# Let's authenticate ourseves to LSEG's Data and Analytics service, Refinitiv:
try: # The following libraries are not available in Codebook, thus this try loop
rd.open_session(config_name="C:\\Example.DataLibrary.Python-main\\Example.DataLibrary.Python-main\\Configuration\\refinitiv-data.config.json")
rd.open_session("desktop.workspace")
except:
rd.open_session()
print(f"Here we are using the refinitiv Data Library version {rd.__version__}")
In this article, we will attempt to calculate the Implied Volatility (IV) for Future Options on 2 indexes (.STOXX50E & .SPX) trading 'ATM', meaning that the contract's strike price is at (or near - within x%) parity with (equal to) its current treading price (TRDPRC_1). We are also only looking for such Options expiring within a set time window; allowing for the option 'forever', i.e.: that expire whenever after date of calculation. To do so, we 1st have to find the option in question. To find live Options, we best use the Search API. To find Expired Options we will use functions created in Haykaz's amazing articles "Finding Expired Options and Backtesting a Short Iron Condor Strategy" & "Functions to find Option RICs traded on different exchanges"
Live Options, in this context, are Options that have not expired at time of computation. To be explicit:
As aforementioned, to find live Options, we best use the Search API: Here we look for options on .STOXX50E that mature on the 3rd friday of July 2023, 2023-07-21:
response1 = search.Definition(
view = search.Views.SEARCH_ALL, # To see what views are available: `help(search.Views)` & `search.metadata.Definition(view = search.Views.SEARCH_ALL).get_data().data.df.to_excel("SEARCH_ALL.xlsx")`
query=".STOXX50E",
select="DocumentTitle, RIC, StrikePrice, ExchangeCode, ExpiryDate, UnderlyingAsset, " +
"UnderlyingAssetName, UnderlyingAssetRIC, ESMAUnderlyingIndexCode, RCSUnderlyingMarket" +
"UnderlyingQuoteName, UnderlyingQuoteRIC",
filter="RCSAssetCategoryLeaf eq 'Option' and RIC eq 'STX*' and DocumentTitle ne '*Weekly*' " +
"and CallPutOption eq 'Call' and ExchangeCode eq 'EUX' and " +
"ExpiryDate ge 2022-07-10 and ExpiryDate lt 2023-07-22", # ge (greater than or equal to), gt (greater than), lt (less than) and le (less than or equal to). These can only be applied to numeric and date properties.
top=100,
).get_data()
searchDf1 = response1.data.df
searchDf1
Let's say the current underlying price is 3331.7EUR, now we can pick the option with strike price closest to that, i.e.: the most 'At The Money'; note that this means that the option can be in or out the money, as long as it is the closest to at the money:
currentUnderlyingPrc = rd.get_history(
universe=[searchDf1.UnderlyingQuoteRIC[0][0]],
fields=["TRDPRC_1"],
interval="tick").iloc[-1][0]
currentUnderlyingPrc
searchDf1.iloc[(searchDf1['StrikePrice']-currentUnderlyingPrc).abs().argsort()[:1]]
In this instance, for this Call Option, 'STXE33500G3.EX', the strike price is 3350, higher than the spot price of our underlying which is 3331.7. The holder of this 'STXE33500G3.EX' option has the right (but not the obligation) to buy the underlying for 3350EUR, which, was the price of the underlying to stay the same till expiry (3331.7EUR on 2023-07-21), means a loss of (3350 - 3331.7 =) 18.3EUR. This option in this instance is 'Out-The-Money'.
N.B.: When using the Filter in Search and playing with dates, it is good to read the API Playground Documentation; it mentions that: "Dates are written in ISO datetime format. The time portion is optional, as is the timezone (assumed to be UTC unless otherwise specified). Valid examples include 2012-03-11T17\:13:55Z, 2012-03-11T17\:13:55, 2012-03-11T12\:00-03:30, 2012-03-11.":
Most of the time, market agents will be interested in the next expiring Option, unless we are too close to it. We would not be interested, for example, in an option expiring in 1 hour, or even tomorrow, because that is so close (in time) that the information reflected in the Option's trades in the market does not represent future expectations of its underlying, but current expectations of it.
To implement such a logic, we need to know what are the expiry dates of the option that we are interested in. We are looking for a Python function narrowing our search to options expiring on the 3rd Friday of any one month. For info on this function, please read articles "Finding Expired Options and Backtesting a Short Iron Condor Strategy" & "Functions to find Option RICs traded on different exchanges"
def Get_exp_dates(year, days=True, mcal_get_calendar='EUREX'):
'''
Get_exp_dates Version 2.0:
This function gets expiration dates for a year for NDX options, which are the 3rd Fridays of each month.
Changes
----------------------------------------------
Changed from Version 1.0 to 2.0: Jonathan Legrand changed Haykaz Aramyan's original code to allow
(i) for the function's holiday argument to be changed, and defaulted to 'EUREX' as opposed to 'CBOE_Index_Options' and
(ii) for the function to output full date objects as opposed to just days of the month if agument days=True.
Dependencies
----------------------------------------------
Python library 'pandas_market_calendars' version 3.2
Parameters
-----------------------------------------------
Input:
year(int): year for which expiration days are requested
mcal_get_calendar(str): String of the calendar for which holidays have to be taken into account. More on this calendar (link to Github chacked 2022-10-11): https://github.com/rsheftel/pandas_market_calendars/blob/177e7922c7df5ad249b0d066b5c9e730a3ee8596/pandas_market_calendars/exchange_calendar_cboe.py
Default: mcal_get_calendar='EUREX'
days(bool): If True, only days of the month is outputed, else it's dataeime objects
Default: days=True
Output:
dates(dict): dictionary of expiration days for each month of a specified year in datetime.date format.
'''
# get CBOE market holidays
EUREXCal = mcal.get_calendar(mcal_get_calendar)
holidays = EUREXCal.holidays().holidays
# set calendar starting from Saturday
c = calendar.Calendar(firstweekday=calendar.SATURDAY)
# get the 3rd Friday of each month
exp_dates = {}
for i in range(1, 13):
monthcal = c.monthdatescalendar(year, i)
date = monthcal[2][-1]
# check if found date is an holiday and get the previous date if it is
if date in holidays:
date = date + timedelta(-1)
# append the date to the dictionary
if year in exp_dates:
### Changed from original code from here on by Jonathan Legrand on 2022-10-11
if days: exp_dates[year].append(date.day)
else: exp_dates[year].append(date)
else:
if days: exp_dates[year] = [date.day]
else: exp_dates[year] = [date]
return exp_dates
fullDates = Get_exp_dates(2022, days=False)
dates = Get_exp_dates(2022)
fullDatesStrDict = {i: [fullDates[i][j].strftime('%Y-%m-%d')
for j in range(len(fullDates[i]))]
for i in list(fullDates.keys())}
fullDatesDayDict = {i: [fullDates[i][j].day
for j in range(len(fullDates[i]))]
for i in list(fullDates.keys())}
print(fullDates)
print(fullDatesStrDict)
print(dates)
print(fullDatesDayDict)
Most of the time, market agents will be interested in the next expiring Option, unless we are too close to it. We would not be interested, for example, in an option expiring in 1 hour, or even tomorrow, because that is so close (in time) that the information reflected in the Option's trades in the market does not represent future expectations of its underlying, but current expectations of it.
E.g.: I would like to know what is the next Future (Monthly) Option (i) on the Index '.STOXX50E' (ii) closest to ATM (i.e.: with an underlying spot price closest to the option's strike price) (ii) Expiring in more than x days (i.e.: not too close to calculated time 't'), let's say 15 days:
x = 15
timeOfCalcDatetime = datetime.now() # For now, we will focuss on the use-case where we are calculating values for today; later we will allow for it historically for any day going back a few business days.
timeOfCalcStr = datetime.now().strftime('%Y-%m-%d')
timeOfCalcStr
fullDatesAtTimeOfCalc = Get_exp_dates(timeOfCalcDatetime.year, days=False) # `timeOfCalcDatetime.year` here is 2023
fullDatesAtTimeOfCalcDatetime = [
datetime(i.year, i.month, i.day)
for i in fullDatesAtTimeOfCalc[list(fullDatesAtTimeOfCalc.keys())[0]]]
print(fullDatesAtTimeOfCalcDatetime)
expiryDateOfInt = [i for i in fullDatesAtTimeOfCalcDatetime
if i > timeOfCalcDatetime + relativedelta(days=x)][0]
expiryDateOfInt
Now we can look for the one option we're after:
response2 = search.Definition(
view=search.Views.SEARCH_ALL, # To see what views are available: `help(search.Views)` & `search.metadata.Definition(view = search.Views.SEARCH_ALL).get_data().data.df.to_excel("SEARCH_ALL.xlsx")`
query=".STOXX50E",
select="DocumentTitle, RIC, StrikePrice, ExchangeCode, ExpiryDate, UnderlyingAsset, " +
"UnderlyingAssetName, UnderlyingAssetRIC, ESMAUnderlyingIndexCode, RCSUnderlyingMarket" +
"UnderlyingQuoteName, UnderlyingQuoteRIC",
filter="RCSAssetCategoryLeaf eq 'Option' and RIC eq 'STX*' and DocumentTitle ne '*Weekly*' " +
"and CallPutOption eq 'Call' and ExchangeCode eq 'EUX' and " +
f"ExpiryDate ge {(expiryDateOfInt - relativedelta(days=1)).strftime('%Y-%m-%d')} " +
f"and ExpiryDate lt {(expiryDateOfInt + relativedelta(days=1)).strftime('%Y-%m-%d')}", # ge (greater than or equal to), gt (greater than), lt (less than) and le (less than or equal to). These can only be applied to numeric and date properties.
top=10000,
).get_data()
searchDf2 = response2.data.df
searchDf2
And again, we can collect the closest to ATM:
searchDf2.iloc[(searchDf2['StrikePrice']-currentUnderlyingPrc).abs().argsort()[:1]]
Now we have our instrument:
instrument = searchDf2.iloc[(searchDf2['StrikePrice']-currentUnderlyingPrc).abs().argsort()[:1]].RIC.values[0]
instrument
Refinitiv provides pre-calculated Implied Volatility values, but they are daily, and we will look into calculating them in higher frequencies:
## Example Options:
# instrument_1 = 'SPXv212240000.U'
# instrument_2 = 'STXE35500J2.EX' # Eurex Dow Jones EURO STOXX 50 Index Option 3550 Call Oct 2022, Stock Index Cash Option, Underlying RIC: .STOXX50E
# instrument_3 = 'SPXj212240000.U'
datetime.now().isoformat(timespec='minutes')
start = (timeOfCalcDatetime - pd.tseries.offsets.BDay(5)).strftime('%Y-%m-%dT%H:%M:%S.%f') # '2022-10-05T07:30:00.000'
endDateTime = datetime.now()
end = endDateTime.strftime('%Y-%m-%dT%H:%M:%S.%f') # e.g.: '2022-09-09T20:00:00.000'
end
_RefDailyImpVolDf = historical_pricing.events.Definition(
instrument, fields=['IMP_VOLT'], count=2000).get_data()
_RefDailyImpVolDf.data.df.head()
try: RefDailyImpVolDf = _RefDailyImpVolDf.data.df.drop(['EVENT_TYPE'], axis=1) # In codebook, this line is needed
except: RefDailyImpVolDf = _RefDailyImpVolDf.data.df # If outside of codebook
fig = px.line(RefDailyImpVolDf, title = RefDailyImpVolDf.columns.name + " " + RefDailyImpVolDf.columns[0]) # This is just to see the implied vol graph when that field is available
fig.show()
# rd.get_history(
# universe=["STXE35500J2.EX"],
# fields=["TRDPRC_1"],
# interval="tick")
_optnMrktPrice = rd.get_history(
universe=[instrument],
fields=["TRDPRC_1"],
interval="10min",
start=start, # Ought to always start at 4 am for OPRA exchanged Options, more info in the article below
end=end) # Ought to always end at 8 pm for OPRA exchanged Options, more info in the article below
As you can see, there isn't nessesarily a trade every 10 min.:
_optnMrktPrice.head()
However, for the statistical inferences that we will make further in the article, when we will calculate Implied Volatilities and therefore implement the Black Scholes model, we will need 'continuous timeseries' with which to deal. There are several ways to go from discrete time series (like ours, even if we go down to tick data), but for this article, we will 1st focus on making 'buckets' of 10 min. If no trade is made in any 10 min. bucket, we will assume the price to have stayed the same as previously, throughout the exchange's trading hours which are:
thankfully this is simple. Let's stick with the EUREX for now:
optnMrktPrice = _optnMrktPrice.resample('10Min').mean() # get a datapoint every 10 min
optnMrktPrice = optnMrktPrice[optnMrktPrice.index.strftime('%Y-%m-%d').isin([i for i in _optnMrktPrice.index.strftime('%Y-%m-%d').unique()])] # Only keep trading days
optnMrktPrice = optnMrktPrice.loc[(optnMrktPrice.index.strftime('%H:%M:%S') >= '07:30:00') & (optnMrktPrice.index.strftime('%H:%M:%S') <= '22:00:00')] # Only keep trading hours
optnMrktPrice.fillna(method='ffill', inplace=True) # Forward Fill to populate NaN values
print(f"Our dataframe started at {str(optnMrktPrice.index[0])} and went on continuously till {str(optnMrktPrice.index[-1])}, so out of trading hours rows are removed")
optnMrktPrice
Note that the option might not have traded in the past 10 min. This can cause issues in the code below, we thus ought to add a row for the current time:
# optnMrktPrice = optnMrktPrice.append(
# pd.DataFrame(
# [[pd.NA]], columns=optnMrktPrice.columns,
# index=[(endDateTime + (datetime.min - endDateTime) % timedelta(minutes=10))]))
# optnMrktPrice
Note also that one may want to only look at 'At Option Trade' datapoints, i.e.: Implied Volatility when a trade is made for the Option, but not when none is made. For this, we will use the 'At Trade' (AT) dataframes:
AToptnMrktPrice = _optnMrktPrice
AToptnMrktPrice
Now let's get data for the underying, which we need to calculate IV:
underlying = searchDf2.iloc[(searchDf2['StrikePrice']-currentUnderlyingPrc).abs().argsort()[:1]].UnderlyingQuoteRIC.values[0][0]
underlying
If you are interested in the opening times of any one exchange, you can use the following:
hoursDf = rd.get_data(universe=["EUREX21"],
fields=["ROW80_10"])
display(hoursDf)
hoursDf.iloc[0,1]
_underlyingMrktPrice = rd.get_history(
universe=[underlying],
fields=["TRDPRC_1"],
interval="10min",
start=start,
end=end)
_underlyingMrktPrice
ATunderlyingMrktPrice = AToptnMrktPrice.join(
_underlyingMrktPrice, lsuffix='_OptPr', rsuffix='_UnderlyingPr', how='inner')
ATunderlyingMrktPrice
Let's put it all in one data-frame, df. Some datasets will have data going from the time we sort for start all the way to end. Some won't because no trade happened in the past few minutes/hours. We ought to base ourselves on the dataset with values getting closer to end and ffill for the other column. As a result, the following if loop is needed:
if optnMrktPrice.index[-1] >= _underlyingMrktPrice.index[-1]:
df = optnMrktPrice.copy()
df['underlying ' + underlying + ' TRDPRC_1'] = _underlyingMrktPrice
else:
df = _underlyingMrktPrice.copy()
df.rename(columns={"TRDPRC_1": 'underlying ' + underlying + ' TRDPRC_1'}, inplace=True)
df['TRDPRC_1'] = optnMrktPrice
df.columns.name = optnMrktPrice.columns.name
df.fillna(method='ffill', inplace=True) # Forward Fill to populate NaN values
df = df.dropna()
df
ATdf = ATunderlyingMrktPrice
strikePrice = searchDf2.iloc[(searchDf2['StrikePrice']-currentUnderlyingPrc).abs().argsort()[:1]].StrikePrice.values[0]
strikePrice
_EurRfRate = rd.get_history(
universe=['EURIBOR3MD='], # USD3MFSR=, USDSOFR=
fields=['TR.FIXINGVALUE'],
# Since we will use `dropna()` as a way to select the rows we are after later on in the code, we need to ask for more risk-free data than needed, just in case we don't have enough:
start=(datetime.strptime(start, '%Y-%m-%dT%H:%M:%S.%f') - timedelta(days=1)).strftime('%Y-%m-%d'),
end=(datetime.strptime(end, '%Y-%m-%dT%H:%M:%S.%f') + timedelta(days=1)).strftime('%Y-%m-%d'))
_EurRfRate
Euribor values are released daily at 11am CET, and it is published as such on Refinitiv:
EurRfRate = _EurRfRate.resample('10Min').mean().fillna(method='ffill')
df['EurRfRate'] = EurRfRate
df = df.fillna(method='ffill')
df
Now for the At Trade dataframe:
pd.options.mode.chained_assignment = None # default='warn'
ATunderlyingMrktPrice['EurRfRate'] = [pd.NA for i in ATunderlyingMrktPrice.index]
for i in _EurRfRate.index:
_i = str(i)[:10]
for n, j in enumerate(ATunderlyingMrktPrice.index):
if _i in str(j):
if len(_EurRfRate.loc[i].values) == 2:
ATunderlyingMrktPrice['EurRfRate'].iloc[n] = _EurRfRate.loc[i].values[0][0]
elif len(_EurRfRate.loc[i].values) == 1:
ATunderlyingMrktPrice['EurRfRate'].iloc[n] = _EurRfRate.loc[i].values[0]
ATdf = ATunderlyingMrktPrice.copy()
ATdf
We are going to assume no dividends.
On the Developer Portal, one can see documentation about the Instrument Pricing Analytics service that allows access to calculating functions (that use to be called 'AdFin'). This service is accessible via several RESTful endpoints (in a family of endpoints called 'Quantitative Analytics') which can be used via RD:
Data returned this far was time-stamped in the GMT Time Zone, we need to re-calibrate it to the timezone of our machine:
dfGMT = df.copy()
dfLocalTimeZone = df.copy()
dfLocalTimeZone.index = [
df.index[i].replace(
tzinfo=pytz.timezone(
'GMT')).astimezone(
tz=datetime.now().astimezone().tzinfo)
for i in range(len(df))]
dfGMT
dfLocalTimeZone
Now for the At Trade dataframe:
ATdfGMT = ATdf.copy()
ATdfLocalTimeZone = ATdf.copy()
ATdfLocalTimeZone.index = [
ATdf.index[i].replace(
tzinfo=pytz.timezone(
'GMT')).astimezone(
tz=datetime.now().astimezone().tzinfo)
for i in range(len(ATdf))]
ATdfGMT
ATdfLocalTimeZone
universeL = [
{
"instrumentType": "Option",
"instrumentDefinition": {
"buySell": "Buy",
"underlyingType": "Eti",
"instrumentCode": instrument,
"strike": str(strikePrice),
},
"pricingParameters": {
"marketValueInDealCcy": str(dfLocalTimeZone['TRDPRC_1'][i]),
"riskFreeRatePercent": str(dfLocalTimeZone['EurRfRate'][i]),
"underlyingPrice": str(dfLocalTimeZone['underlying ' + underlying + ' TRDPRC_1'][i]),
"pricingModelType": "BlackScholes",
"dividendType": "ImpliedYield",
"volatilityType": "Implied",
"underlyingTimeStamp": "Default",
"reportCcy": "EUR"
}
}
for i in range(len(dfLocalTimeZone.index))]
ATuniverseL = [
{
"instrumentType": "Option",
"instrumentDefinition": {
"buySell": "Buy",
"underlyingType": "Eti",
"instrumentCode": instrument,
"strike": str(strikePrice),
},
"pricingParameters": {
"marketValueInDealCcy": str(ATdfLocalTimeZone['TRDPRC_1_OptPr'][i]),
"riskFreeRatePercent": str(ATdfLocalTimeZone['EurRfRate'][i]),
"underlyingPrice": str(ATdfLocalTimeZone['TRDPRC_1_UnderlyingPr'][i]),
"pricingModelType": "BlackScholes",
"dividendType": "ImpliedYield",
"volatilityType": "Implied",
"underlyingTimeStamp": "Default",
"reportCcy": "EUR"
}
}
for i in range(len(ATdfLocalTimeZone.index))]
def Chunks(lst, n):
"""Yield successive n-sized chunks from lst."""
for i in range(0, len(lst), n):
yield lst[i:i + n]
requestFields = [
"MarketValueInDealCcy", "RiskFreeRatePercent",
"UnderlyingPrice", "PricingModelType",
"DividendType", "VolatilityType",
"UnderlyingTimeStamp", "ReportCcy",
"VolatilityType", "Volatility",
"DeltaPercent", "GammaPercent",
"RhoPercent", "ThetaPercent",
"VegaPercent"]
for i, j in enumerate(Chunks(universeL, 100)):
print(f"Batch of (100 or fewer) requests no.: {str(i+1)}/{str(len([i for i in Chunks(universeL, 100)]))}")
# Example request with Body Parameter - Symbology Lookup
request_definition = rd.delivery.endpoint_request.Definition(
method=rd.delivery.endpoint_request.RequestMethod.POST,
url='https://api.refinitiv.com/data/quantitative-analytics/v1/financial-contracts',
body_parameters={"fields": requestFields,
"outputs": ["Data", "Headers"],
"universe": j})
response3 = request_definition.get_data()
headers_name = [h['name'] for h in response3.data.raw['headers']]
if i == 0:
response3df = pd.DataFrame(data=response3.data.raw['data'], columns=headers_name)
else:
_response3df = pd.DataFrame(data=response3.data.raw['data'], columns=headers_name)
response3df = response3df.append(_response3df, ignore_index=True)
response3df
for i, j in enumerate(Chunks(ATuniverseL, 100)):
print(f"Batch of (100 or fewer) requests no.: {str(i+1)}/{str(len([i for i in Chunks(ATuniverseL, 100)]))}")
# Example request with Body Parameter - Symbology Lookup
ATrequest_definition = rd.delivery.endpoint_request.Definition(
method=rd.delivery.endpoint_request.RequestMethod.POST,
url='https://api.refinitiv.com/data/quantitative-analytics/v1/financial-contracts',
body_parameters={"fields": requestFields,
"outputs": ["Data", "Headers"],
"universe": j})
ATresponse3 = ATrequest_definition.get_data()
ATheaders_name = [h['name'] for h in ATresponse3.data.raw['headers']]
if i == 0:
ATresponse3df = pd.DataFrame(data=ATresponse3.data.raw['data'], columns=ATheaders_name)
else:
_ATresponse3df = pd.DataFrame(data=ATresponse3.data.raw['data'], columns=ATheaders_name)
ATresponse3df = ATresponse3df.append(_ATresponse3df, ignore_index=True)
ATresponse3df
IPADf, ATIPADf = response3df.copy(), ATresponse3df.copy() # IPA here stands for the service we used to get all the calculated valuse, Instrument Pricint Analitycs.
IPADf.index, ATIPADf.index = dfLocalTimeZone.index, ATdfLocalTimeZone.index
IPADf.columns.name = dfLocalTimeZone.columns.name
ATIPADf.columns.name = ATdfLocalTimeZone.columns.name
IPADf.rename(columns={"Volatility": 'ImpliedVolatility'}, inplace=True)
ATIPADf.rename(columns={"Volatility": 'ImpliedVolatility'}, inplace=True)
IPADf
ATIPADf
From now on we will not show AT dataframe equivalents because it is... equivalent!
display(searchDf2.iloc[(searchDf2['StrikePrice']-currentUnderlyingPrc).abs().argsort()[:1]])
IPADfGraph = IPADf[['ImpliedVolatility', 'MarketValueInDealCcy',
'RiskFreeRatePercent', 'UnderlyingPrice', 'DeltaPercent',
'GammaPercent', 'RhoPercent', 'ThetaPercent', 'VegaPercent']]
fig = px.line(IPADfGraph) # This is just to see the implied vol graph when that field is available
# fig.layout = dict(xaxis=dict(type="category"))
# Format Graph: https://plotly.com/python/tick-formatting/
fig.update_layout(
title=instrument,
template='plotly_dark')
# Make it so that only one line is shown by default: # https://stackoverflow.com/questions/73384807/plotly-express-plot-subset-of-dataframe-columns-by-default-and-the-rest-as-opt
fig.for_each_trace(
lambda t: t.update(
visible=True if t.name in IPADfGraph.columns[:1] else "legendonly"))
# fig.update_xaxes(autorange=True)
# fig.update_layout(yaxis=IPADf.index[0::10])
fig.show()
fig = make_subplots(rows=3, cols=1)
fig.add_trace(go.Scatter(x=IPADf.index, y=IPADf.ImpliedVolatility, name='Op Imp Volatility'), row=1, col=1)
fig.add_trace(go.Scatter(x=IPADf.index, y=IPADf.MarketValueInDealCcy, name='Op Mk Pr'), row=2, col=1)
fig.add_trace(go.Scatter(x=IPADf.index, y=IPADf.UnderlyingPrice, name=underlying+' Undrlyg Pr'), row=3, col=1)
fig.update(layout_xaxis_rangeslider_visible=False)
fig.update_layout(title=IPADf.columns.name)
fig.update_layout(
template='plotly_dark',
autosize=False,
width=1300,
height=500)
fig.show()
searchDf2.iloc[(searchDf2['StrikePrice']-currentUnderlyingPrc).abs().argsort()[:1]]
Certain companies are slow to update libraries, dependencies or Python versions. They/You may thus not have access to plotly (the graph library we used above). Matplotlib is rather light and should work, even on machines with old setups:
display(searchDf2.iloc[(searchDf2['StrikePrice']-currentUnderlyingPrc).abs().argsort()[:1]])
ATIPADfSimpleGraph = ATIPADf[['ImpliedVolatility']]
fig, axes = plt.subplots(ncols=1)
ax = axes
ax.plot(ATIPADfSimpleGraph.ImpliedVolatility, '.-')
# ax.xaxis.set_major_formatter(ticker.FuncFormatter(format_date))
ax.set_title(f"{searchDf2.iloc[(searchDf2['StrikePrice']-currentUnderlyingPrc).abs().argsort()[:1]].RIC.values[0]} Implied Volatility At Trade Only")
fig.autofmt_xdate()
plt.show()
Let's put it all together into a single function. This ImpVolatilityCalcIPA function will allow anyone to:
(I) find the option (i) with the index of your choice (SPX or EUREX) as underlying, (ii) closest to strike price right now (i.e.: At The Money) and (iii) with the next, closest expiry date past x days after today,
(II) calculate the Implied Volatility for that option either (i) only at times when the option itself is traded or (ii) at any time the option or the underlying is being traded.
def ImpVolatilityCalcIPA(x=15,
indexUnderlying=".STOXX50E",
callOrPut='Put',
dateBack=3,
expiryYearOfInterest=datetime.now().year,
riskFreeRate=None, riskFreeRateField=None,
timeZoneInGraph=datetime.now().astimezone(),
maxColwidth=200,
graphStyle='without out of trading hours', # 'with out of trading hours', '3 graphs', 'simple'
simpleGraphLineStyle='.-', # 'o-'
simpleGraphSize=(15, 5),
graphTemplate='plotly_dark',
debug=False,
returnDfGraph=False,
AtOptionTradeOnly=False):
if indexUnderlying == ".STOXX50E":
exchangeC, exchangeRIC, mcalGetCalendar = 'EUX', 'STX', 'EUREX'
elif indexUnderlying == '.SPX':
exchangeC, exchangeRIC, mcalGetCalendar = 'OPQ', 'SPX', 'CBOE_Futures'# 'CBOE_Index_Options' # should be 'CBOE_Index_Options'... CBOT_Equity
def Get_exp_dates(year=expiryYearOfInterest,
days=True,
mcal_get_calendar=mcalGetCalendar):
'''
Get_exp_dates Version 3.0:
This function gets expiration dates for a year for NDX options, which are the 3rd Fridays of each month.
Changes
----------------------------------------------
Changed from Version 1.0 to 2.0: Jonathan Legrand changed Haykaz Aramyan's original code to allow
(i) for the function's holiday argument to be changed, and defaulted to 'EUREX' as opposed to 'CBOE_Index_Options' and
(ii) for the function to output full date objects as opposed to just days of the month if agument days=True.
Changed from Version 2.0 to 3.0: Jonathan Legrand changed this function to reflec tthe fact that it can be used for indexes other than EUREX.
Dependencies
----------------------------------------------
Python library 'pandas_market_calendars' version 3.2
Parameters
-----------------------------------------------
Input:
year(int): year for which expiration days are requested
mcal_get_calendar(str): String of the calendar for which holidays have to be taken into account. More on this calendar (link to Github chacked 2022-10-11): https://github.com/rsheftel/pandas_market_calendars/blob/177e7922c7df5ad249b0d066b5c9e730a3ee8596/pandas_market_calendars/exchange_calendar_cboe.py
Default: mcal_get_calendar='EUREX'
days(bool): If True, only days of the month is outputed, else it's dataeime objects
Default: days=True
Output:
dates(dict): dictionary of expiration days for each month of a specified year in datetime.date format.
'''
# get CBOE market holidays
Cal = mcal.get_calendar(mcal_get_calendar)
holidays = Cal.holidays().holidays
# set calendar starting from Saturday
c = calendar.Calendar(firstweekday=calendar.SATURDAY)
# get the 3rd Friday of each month
exp_dates = {}
for i in range(1, 13):
monthcal = c.monthdatescalendar(year, i)
date = monthcal[2][-1]
# check if found date is an holiday and get the previous date if it is
if date in holidays:
date = date + timedelta(-1)
# append the date to the dictionary
if year in exp_dates:
### Changed from original code from here on by Jonathan Legrand on 2022-10-11
if days: exp_dates[year].append(date.day)
else: exp_dates[year].append(date)
else:
if days: exp_dates[year] = [date.day]
else: exp_dates[year] = [date]
return exp_dates
timeOfCalcDatetime = datetime.now() # For now, we will focuss on the use-case where we are calculating values for today; later we will allow for it historically for any day going back a few business days.
timeOfCalcStr = datetime.now().strftime('%Y-%m-%d')
fullDatesAtTimeOfCalc = Get_exp_dates(timeOfCalcDatetime.year, days=False) # `timeOfCalcDatetime.year` here is 2023
fullDatesAtTimeOfCalcDatetime = [
datetime(i.year, i.month, i.day)
for i in fullDatesAtTimeOfCalc[list(fullDatesAtTimeOfCalc.keys())[0]]]
expiryDateOfInt = [i for i in fullDatesAtTimeOfCalcDatetime
if i > timeOfCalcDatetime + relativedelta(days=x)][0]
if debug: print(f"expiryDateOfInt: {expiryDateOfInt}")
response = search.Definition(
view = search.Views.SEARCH_ALL, # To see what views are available: `help(search.Views)` & `search.metadata.Definition(view = search.Views.SEARCH_ALL).get_data().data.df.to_excel("SEARCH_ALL.xlsx")`
query=indexUnderlying,
select="DocumentTitle, RIC, StrikePrice, ExchangeCode, ExpiryDate, UnderlyingAsset, " +
"UnderlyingAssetName, UnderlyingAssetRIC, ESMAUnderlyingIndexCode, RCSUnderlyingMarket" +
"UnderlyingQuoteName, UnderlyingQuoteRIC",
filter=f"RCSAssetCategoryLeaf eq 'Option' and RIC eq '{exchangeRIC}*' and DocumentTitle ne '*Weekly*' " +
f"and CallPutOption eq '{callOrPut}' and ExchangeCode eq '{exchangeC}' and " +
f"ExpiryDate ge {(expiryDateOfInt - relativedelta(days=1)).strftime('%Y-%m-%d')} " +
f"and ExpiryDate lt {(expiryDateOfInt + relativedelta(days=1)).strftime('%Y-%m-%d')}", # ge (greater than or equal to), gt (greater than), lt (less than) and le (less than or equal to). These can only be applied to numeric and date properties.
top=10000,
).get_data()
searchDf = response.data.df
if debug: display(searchDf)
try:
underlyingPrice = rd.get_history(
universe=[indexUnderlying],
fields=["TRDPRC_1"],
interval="tick").iloc[-1][0]
except:
print("Function failed at the search strage, returning the following dataframe: ")
display(searchDf)
if debug:
print(f"Underlying {indexUnderlying}'s price recoprded here was {underlyingPrice}")
display(searchDf.iloc[(searchDf['StrikePrice']-underlyingPrice).abs().argsort()[:10]])
instrument = searchDf.iloc[(searchDf['StrikePrice']-underlyingPrice).abs().argsort()[:1]].RIC.values[0]
start = (timeOfCalcDatetime - pd.tseries.offsets.BDay(dateBack)).strftime('%Y-%m-%dT%H:%M:%S.%f') # '2022-10-05T07:30:00.000'
endDateTime = datetime.now()
end = endDateTime.strftime('%Y-%m-%dT%H:%M:%S.%f') # e.g.: '2022-09-09T20:00:00.000'
_optnMrktPrice = rd.get_history(
universe=[instrument],
fields=["TRDPRC_1"],
interval="10min",
start=start, # Ought to always start at 4 am for OPRA exchanged Options, more info in the article below
end=end) # Ought to always end at 8 pm for OPRA exchanged Options, more info in the article below
if debug:
print(instrument)
display(_optnMrktPrice)
## Data on certain options are stale and do not nessesarily show up on Workspace, in case that happens, we will pick the next ATM Option, which probably will have the same strike, but we will only do so once, any more and we could get too far from strike:
if _optnMrktPrice.empty:
if debug: print(f"No data could be found for {instrument}, so the next ATM Option was chosen")
instrument = searchDf.iloc[(searchDf['StrikePrice']-underlyingPrice).abs().argsort()[1:2]].RIC.values[0]
if debug: print(f"{instrument}")
_optnMrktPrice = rd.get_history(universe=[instrument],
fields=["TRDPRC_1"], interval="10min",
start=start, end=end)
if debug: display(_optnMrktPrice)
if _optnMrktPrice.empty: # Let's try one more time, as is often nessesary
if debug: print(f"No data could be found for {instrument}, so the next ATM Option was chosen")
instrument = searchDf.iloc[(searchDf['StrikePrice']-underlyingPrice).abs().argsort()[2:3]].RIC.values[0]
if debug: print(f"{instrument}")
_optnMrktPrice = rd.get_history(universe=[instrument],
fields=["TRDPRC_1"], interval="10min",
start=start, end=end)
if debug: display(_optnMrktPrice)
if _optnMrktPrice.empty:
print(f"No data could be found for {instrument}, please check it on Refinitiv Workspace")
optnMrktPrice = _optnMrktPrice.resample('10Min').mean() # get a datapoint every 10 min
optnMrktPrice = optnMrktPrice[optnMrktPrice.index.strftime('%Y-%m-%d').isin([i for i in _optnMrktPrice.index.strftime('%Y-%m-%d').unique()])] # Only keep trading days
optnMrktPrice = optnMrktPrice.loc[(optnMrktPrice.index.strftime('%H:%M:%S') >= '07:30:00') & (optnMrktPrice.index.strftime('%H:%M:%S') <= '22:00:00')] # Only keep trading hours
optnMrktPrice.fillna(method='ffill', inplace=True) # Forward Fill to populate NaN values
# Note also that one may want to only look at 'At Option Trade' datapoints,
# i.e.: Implied Volatility when a trade is made for the Option, but not when
# none is made. For this, we will use the 'At Trade' (`AT`) dataframes:
if AtOptionTradeOnly: AToptnMrktPrice = _optnMrktPrice
underlying = searchDf.iloc[(searchDf['StrikePrice']).abs().argsort()[:1]].UnderlyingQuoteRIC.values[0][0]
_underlyingMrktPrice = rd.get_history(
universe=[underlying],
fields=["TRDPRC_1"],
interval="10min",
start=start,
end=end)
# Let's put it al in one data-frame, `df`. Some datasets will have data
# going from the time we sert for `start` all the way to `end`. Some won't
# because no trade happened in the past few minutes/hours. We ought to base
# ourselves on the dataset with values getting closer to `end` and `ffill`
# for the other column. As a result, the following `if` loop is needed:
if optnMrktPrice.index[-1] >= _underlyingMrktPrice.index[-1]:
df = optnMrktPrice.copy()
df['underlying ' + underlying + ' TRDPRC_1'] = _underlyingMrktPrice
else:
df = _underlyingMrktPrice.copy()
df.rename(
columns={"TRDPRC_1": 'underlying ' + underlying + ' TRDPRC_1'},
inplace=True)
df['TRDPRC_1'] = optnMrktPrice
df.columns.name = optnMrktPrice.columns.name
df.fillna(method='ffill', inplace=True) # Forward Fill to populate NaN values
df = df.dropna()
if AtOptionTradeOnly:
ATunderlyingMrktPrice = AToptnMrktPrice.join(
_underlyingMrktPrice, lsuffix='_OptPr', rsuffix=' Underlying ' + underlying + ' TRDPRC_1', how='inner')
ATdf = ATunderlyingMrktPrice
strikePrice = searchDf.iloc[(searchDf['StrikePrice']-underlyingPrice).abs().argsort()[:1]].StrikePrice.values[0]
if riskFreeRate is None and indexUnderlying == ".SPX":
_riskFreeRate = 'USDCFCFCTSA3M='
_riskFreeRateField = 'TR.FIXINGVALUE'
elif riskFreeRate is None and indexUnderlying == ".STOXX50E":
_riskFreeRate = 'EURIBOR3MD='
_riskFreeRateField = 'TR.FIXINGVALUE'
else:
_riskFreeRate, _riskFreeRateField = riskFreeRate, riskFreeRateField
_RfRate = rd.get_history(
universe=[_riskFreeRate], # USD3MFSR=, USDSOFR=
fields=[_riskFreeRateField],
# Since we will use `dropna()` as a way to select the rows we are after later on in the code, we need to ask for more risk-free data than needed, just in case we don't have enough:
start=(datetime.strptime(start, '%Y-%m-%dT%H:%M:%S.%f') - timedelta(days=1)).strftime('%Y-%m-%d'),
end=(datetime.strptime(end, '%Y-%m-%dT%H:%M:%S.%f') + timedelta(days=1)).strftime('%Y-%m-%d'))
RfRate = _RfRate.resample('10Min').mean().fillna(method='ffill')
df['RfRate'] = RfRate
df = df.fillna(method='ffill')
if AtOptionTradeOnly:
pd.options.mode.chained_assignment = None # default='warn'
ATunderlyingMrktPrice['RfRate'] = [pd.NA for i in ATunderlyingMrktPrice.index]
for i in RfRate.index:
_i = str(i)[:10]
for n, j in enumerate(ATunderlyingMrktPrice.index):
if _i in str(j):
if len(RfRate.loc[i].values)==2:
ATunderlyingMrktPrice['RfRate'].iloc[n] = RfRate.loc[i].values[0][0]
elif len(RfRate.loc[i].values)==1:
ATunderlyingMrktPrice['RfRate'].iloc[n] = RfRate.loc[i].values[0]
ATdf = ATunderlyingMrktPrice.copy()
if timeZoneInGraph != 'GMT':
df.index = [
df.index[i].replace(
tzinfo=pytz.timezone(
'GMT')).astimezone(
tz=timeZoneInGraph.tzinfo)
for i in range(len(df))]
if AtOptionTradeOnly:
ATdf.index = [
ATdf.index[i].replace(
tzinfo=pytz.timezone(
'GMT')).astimezone(
tz=datetime.now().astimezone().tzinfo)
for i in range(len(ATdf))]
if AtOptionTradeOnly:
universeL = [
{
"instrumentType": "Option",
"instrumentDefinition": {
"buySell": "Buy",
"underlyingType": "Eti",
"instrumentCode": instrument,
"strike": str(strikePrice),
},
"pricingParameters": {
"marketValueInDealCcy": str(ATdf['TRDPRC_1_OptPr'][i]),
"riskFreeRatePercent": str(ATdf['RfRate'][i]),
"underlyingPrice": str(ATdf['TRDPRC_1 Underlying ' + underlying + ' TRDPRC_1'][i]),
"pricingModelType": "BlackScholes",
"dividendType": "ImpliedYield",
"volatilityType": "Implied",
"underlyingTimeStamp": "Default",
"reportCcy": "EUR"
}
}
for i in range(len(ATdf.index))]
else:
universeL = [
{
"instrumentType": "Option",
"instrumentDefinition": {
"buySell": "Buy",
"underlyingType": "Eti",
"instrumentCode": instrument,
"strike": str(strikePrice),
},
"pricingParameters": {
"marketValueInDealCcy": str(df['TRDPRC_1'][i]),
"riskFreeRatePercent": str(df['RfRate'][i]),
"underlyingPrice": str(df['underlying ' + underlying + ' TRDPRC_1'][i]),
"pricingModelType": "BlackScholes",
"dividendType": "ImpliedYield",
"volatilityType": "Implied",
"underlyingTimeStamp": "Default",
"reportCcy": "EUR"
}
}
for i in range(len(df.index))]
def Chunks(lst, n):
"""Yield successive n-sized chunks from lst."""
for i in range(0, len(lst), n):
yield lst[i:i + n]
requestFields = [
"MarketValueInDealCcy", "RiskFreeRatePercent",
"UnderlyingPrice", "PricingModelType",
"DividendType", "VolatilityType",
"UnderlyingTimeStamp", "ReportCcy",
"VolatilityType", "Volatility",
"DeltaPercent", "GammaPercent",
"RhoPercent", "ThetaPercent", "VegaPercent"]
for i, j in enumerate(Chunks(universeL, 100)):
# Example request with Body Parameter - Symbology Lookup
request_definition = rd.delivery.endpoint_request.Definition(
method=rd.delivery.endpoint_request.RequestMethod.POST,
url='https://api.refinitiv.com/data/quantitative-analytics/v1/financial-contracts',
body_parameters={
"fields": requestFields,
"outputs": ["Data", "Headers"],
"universe": j})
response2 = request_definition.get_data()
headers_name = [h['name'] for h in response2.data.raw['headers']]
_IPADf = pd.DataFrame(data=response2.data.raw['data'], columns=headers_name)
if i == 0: IPADf = _IPADf
else: IPADf = IPADf.append(_IPADf, ignore_index=True)
if AtOptionTradeOnly:
IPADf.index = ATdf.index
IPADf.columns.name = ATdf.columns.name
else:
IPADf.index = df.index
IPADf.columns.name = df.columns.name
IPADf.rename(columns={"Volatility": 'ImpliedVolatility'}, inplace=True)
# We are going to want to show details about data retreived in a dataframe in the output of this function. The one line below allows us to maximise the width (column) length of cells to see all that is written within them.
pd.options.display.max_colwidth = maxColwidth
if graphStyle=='simple':
display(searchDf.iloc[(searchDf['StrikePrice']-underlyingPrice).abs().argsort()[:1]])
IPADfSimpleGraph = IPADf[['ImpliedVolatility']]
fig, axes = plt.subplots(ncols=1, figsize=simpleGraphSize)
axes.plot(IPADf[['ImpliedVolatility']].ImpliedVolatility, simpleGraphLineStyle)
if AtOptionTradeOnly: axes.set_title(f"{instrument} Implied Volatility At Trade Only")
else: axes.set_title(f"{instrument} Implied Volatility")
plt.show()
else:
display(searchDf.iloc[(searchDf['StrikePrice']-underlyingPrice).abs().argsort()[:1]])
IPADfGraph = IPADf[['ImpliedVolatility', 'MarketValueInDealCcy',
'RiskFreeRatePercent', 'UnderlyingPrice', 'DeltaPercent',
'GammaPercent', 'RhoPercent', 'ThetaPercent', 'VegaPercent']]
if debug: display(IPADfGraph)
try: # This is needed in case there is not enough data to calculate values for all timestamps , see https://stackoverflow.com/questions/67244912/wide-format-csv-with-plotly-express
fig = px.line(IPADfGraph)
except:
if returnDfGraph:
return IPADfGraph
else:
IPADfGraph = IPADfGraph[["ImpliedVolatility","MarketValueInDealCcy","RiskFreeRatePercent","UnderlyingPrice"]]
fig = px.line(IPADfGraph)
if graphStyle=='with out of trading hours':
fig.update_layout(
title=instrument,
template=graphTemplate)
fig.for_each_trace(
lambda t: t.update(
visible=True if t.name in IPADfGraph.columns[:1] else "legendonly"))
fig.show()
elif graphStyle=='3 graphs':
fig = make_subplots(rows=3, cols=1)
fig.add_trace(go.Scatter(x=IPADf.index, y=IPADfGraph.ImpliedVolatility, name='Op Imp Volatility'), row=1, col=1)
fig.add_trace(go.Scatter(x=IPADf.index, y=IPADfGraph.MarketValueInDealCcy, name='Op Mk Pr'), row=2, col=1)
fig.add_trace(go.Scatter(x=IPADf.index, y=IPADfGraph.UnderlyingPrice, name=underlying+' Undrlyg Pr'), row=3, col=1)
fig.update(layout_xaxis_rangeslider_visible=False)
fig.update_layout(title=IPADfGraph.columns.name)
fig.update_layout(
title=instrument,
template=graphTemplate,
autosize=False,
width=1300,
height=500)
fig.show()
else:
print("Looks like the agrument `graphStyle` used is incorrect. Try `simple`, `with out of trading hours` or `3 graphs`")
ImpVolatilityCalcIPA( # This will pick up 10 min data
x=15,
indexUnderlying=".STOXX50E", # ".SPX" or ".STOXX50E"
callOrPut='Put', # 'Put' or 'Call'
dateBack=5,
expiryYearOfInterest=datetime.now().year,
riskFreeRate=None,
riskFreeRateField=None, # 'TR.FIXINGVALUE'
timeZoneInGraph=datetime.now().astimezone(),
maxColwidth=200,
graphStyle='3 graphs', # 'with out of trading hours', '3 graphs', 'simple'
simpleGraphLineStyle='.-', # 'o-'
simpleGraphSize=(15, 5),
graphTemplate='plotly_dark',
debug=False,
returnDfGraph=False,
AtOptionTradeOnly=False)
while True:
# Code executed here
clear_output(wait=True)
try:
try:
ImpVolatilityCalcIPA(
dateBack=3, indexUnderlying=".STOXX50E", callOrPut='Call',
graphStyle='simple', AtOptionTradeOnly=True)
time.sleep(5)
except:
ImpVolatilityCalcIPA(
dateBack=4, # Sometimes, the timewindow for which the request is made is too small to produce values. This could be because of a number of reasons, and the best way round it is to simply ask for a larger time window of data.
indexUnderlying=".STOXX50E", callOrPut='Call',
graphStyle='simple', AtOptionTradeOnly=True)
time.sleep(5)
except:
print("Please wait for next roll")
We are now going to look into using PEP 3107 (and PEP 484) (and some decorators).
# # import refinitiv.data as rd
# # from refinitiv.data.content import historical_pricing
# # from refinitiv.data.content.historical_pricing import Intervals
# # from refinitiv.data.content.historical_pricing import Adjustments
# # from refinitiv.data.content.historical_pricing import MarketSession
# # from refinitiv.data.content import search
# import time
# import numpy as np
# import pandas as pd
# import calendar
# import pytz
# import math
# import pandas_market_calendars as mcal # See `https://github.com/rsheftel/pandas_market_calendars/blob/master/examples/usage.ipynb` for info on this market calendar library
# from datetime import datetime, timedelta, timezone
# from dateutil.relativedelta import relativedelta
# from pandas.tseries.offsets import BDay
# from plotly.subplots import make_subplots
# import plotly.graph_objects as go
# import plotly.express as px # This is just to see the implied vol graph when that field is available
# import matplotlib.pyplot as plt
# import matplotlib.mlab as mlab
# import matplotlib.cbook as cbook
# import matplotlib.ticker as ticker
# from IPython.display import clear_output
# try: # The following libraries are not available in Codebook, thus this try loop
# rd.open_session(config_name="C:\\Example.DataLibrary.Python-main\\Example.DataLibrary.Python-main\\Configuration\\refinitiv-data.config.json")
# rd.open_session("desktop.workspace")
# except:
# rd.open_session()
# print(f"Here we are using the refinitiv Data Library version {rd.__version__}")
%load_ext nb_mypy
%nb_mypy unknown
%nb_mypy On
%nb_mypy DebugOff
from datetime import datetime, timedelta, timezone
from datetime import date as dtdate
from dateutil.relativedelta import relativedelta # to import types: `!pip install types-python-dateutil --trusted-host pypi.org`
import pandas_market_calendars as mcal # See `https://github.com/rsheftel/pandas_market_calendars/blob/master/examples/usage.ipynb` for info on this market calendar library
from typing import Tuple, Union, Dict, List, Any
import numpy as np
# import nb_mypy # !pip3 install nb_mypy --trusted-host pypi.org # https://pypi.org/project/nb-mypy/ # https://gitlab.tue.nl/jupyter-projects/nb_mypy/-/blob/master/Nb_Mypy.ipynb
import calendar
class ImpliedVolatilityIPACalc(): # All about Type Hints here: https://realpython.com/python-type-checking/#static-type-checking
def __init__( # Constroctor
self,
indexUnderlying: str = ".STOXX50E"
):
self.indexUnderlying: str = indexUnderlying
self.dateBack: int = 3
self.expiryYearOfInterest: int = datetime.now().year
self.riskFreeRate: Union[str, None] = None
self.riskFreeRateField: Union[str, None] = None
self.timeZoneInGraph: datetime = datetime.now().astimezone()
self.maxColwidth: int = 200
self.graphStyle: str = 'without out of trading hours' # 'with out of trading hours', '3 graphs', 'simple'
self.simpleGraphLineStyle: str = '.-' # 'o-'
self.simpleGraphSize: Tuple = (15, 5)
self.graphTemplate: str = 'plotly_dark'
self.debug: bool = False
self.returnDfGraph: bool = False
self.atOptionTradeOnly: bool = True
# def change_attrs(self, **kwargs): for kwarg in kwargs: self.__setattr__(kwarg, kwargs[kwarg])
def Get_exp_dates(
self,
year: int = datetime.now().year,
days: bool = True,
mcal_get_calendar: str = 'EUREX'
) -> dict:
'''
Get_exp_dates Version 4.0:
This function gets expiration dates for a year for NDX options, which are the 3rd Fridays of each month.
Changes
----------------------------------------------
Changed from Version 1.0 to 2.0: Jonathan Legrand changed Haykaz Aramyan's original code to allow
(i) for the function's holiday argument to be changed, and defaulted to 'EUREX' as opposed to 'CBOE_Index_Options' and
(ii) for the function to output full date objects as opposed to just days of the month if agument days=True.
Changed from Version 2.0 to 3.0: Jonathan Legrand changed this function to reflec the fact that it can be used for indexes other than EUREX.
Changed from Version 3.0 to 4.0: Jonathan Legrand changed this function to be in line with PEP 3107 (type hints).
Dependencies
----------------------------------------------
Python library 'pandas_market_calendars' version 3.2
Parameters
-----------------------------------------------
Input:
year(int): year for which expiration days are requested
mcal_get_calendar(str): String of the calendar for which holidays have to be taken into account. More on this calendar (link to Github chacked 2022-10-11): https://github.com/rsheftel/pandas_market_calendars/blob/177e7922c7df5ad249b0d066b5c9e730a3ee8596/pandas_market_calendars/exchange_calendar_cboe.py
Default: mcal_get_calendar='EUREX'
days(bool): If True, only days of the month is outputed, else it's dataeime objects
Default: days=True
Output
-----------------------------------------------
dates(dict): dictionary of expiration days for each month of a specified year in datetime.date format.
'''
i: int # this is for the 'for loop' in this function coming below
# get CBOE market holidays
Cal: mcal.get_calendar = mcal.get_calendar(mcal_get_calendar)
holidays: Tuple[np.datetime64, ...] = Cal.holidays().holidays
# set calendar starting from Saturday
c: calendar.Calendar = calendar.Calendar(firstweekday=calendar.SATURDAY)
# get the 3rd Friday of each month
exp_dates: dict = {} # https://stackoverflow.com/questions/48054521/indicating-multiple-value-in-a-dict-for-type-hints
date: dtdate
for i in range(1, 13):
date = c.monthdatescalendar(year, i)[2][-1]
# check if found date is an holiday and get the previous date if it is
if date in holidays:
date = date + timedelta(-1)
# append the date to the dictionary
if year in exp_dates and days:
exp_dates[year].append(date.day)
elif year in exp_dates:
exp_dates[year].append(date)
elif days:
exp_dates[year] = [date.day]
else:
exp_dates[year] = [date]
return exp_dates
def get(
self,
debug: bool = False,
after: int = 15,
callOrPut: str = 'Put'
) -> ImpliedVolatilityIPACalc:
self.after = after
i: int # this is for the 'for loop' in this function coming below
self.exchangeC: str
self.exchangeRIC: str
self.mcalGetCalendar: str
if self.indexUnderlying == ".STOXX50E":
self.exchangeC, self.exchangeRIC, self.mcalGetCalendar = 'EUX', 'STX', 'EUREX'
elif self.indexUnderlying == '.SPX':
self.exchangeC, self.exchangeRIC, self.mcalGetCalendar = 'OPQ', 'SPX', 'CBOE_Futures' # 'CBOE_Index_Options' # should be 'CBOE_Index_Options'... CBOT_Equity
timeOfCalcDatetime : datetime = datetime.now() # For now, we will focuss on the use-case where we are calculating values for today; later we will allow for it historically for any day going back a few business days.
timeOfCalcStr: str = datetime.now().strftime('%Y-%m-%d')
fullDatesAtTimeOfCalc: dict = self.Get_exp_dates(
year=timeOfCalcDatetime.year,
days=False,
mcal_get_calendar=self.mcalGetCalendar)
fullDatesAtTimeOfCalcDatetime: List[datetime] = [
datetime(i.year, i.month, i.day)
for i in fullDatesAtTimeOfCalc[list(fullDatesAtTimeOfCalc.keys())[0]]]
expiryDateOfInt: List[datetime] = [
i for i in fullDatesAtTimeOfCalcDatetime
if i > timeOfCalcDatetime + relativedelta(days=self.after)][0]
if self.debug: print(f"expiryDateOfInt: {expiryDateOfInt}")
response = search.Definition(
view=search.Views.SEARCH_ALL, # To see what views are available: `help(search.Views)` & `search.metadata.Definition(view = search.Views.SEARCH_ALL).get_data().data.df.to_excel("SEARCH_ALL.xlsx")`
query=self.indexUnderlying,
select="DocumentTitle, RIC, StrikePrice, ExchangeCode, ExpiryDate, UnderlyingAsset, " +
"UnderlyingAssetName, UnderlyingAssetRIC, ESMAUnderlyingIndexCode, RCSUnderlyingMarket" +
"UnderlyingQuoteName, UnderlyingQuoteRIC",
filter=f"RCSAssetCategoryLeaf eq 'Option' and RIC eq '{self.exchangeRIC}*' and DocumentTitle ne '*Weekly*' " +
f"and CallPutOption eq '{callOrPut}' and ExchangeCode eq '{self.exchangeC}' and " +
f"ExpiryDate ge {(expiryDateOfInt - relativedelta(days=1)).strftime('%Y-%m-%d')} " +
f"and ExpiryDate lt {(expiryDateOfInt + relativedelta(days=1)).strftime('%Y-%m-%d')}", # ge (greater than or equal to), gt (greater than), lt (less than) and le (less than or equal to). These can only be applied to numeric and date properties.
top=10000,
).get_data()
searchDf = response.data.df
return self
test = ImpliedVolatilityIPACalc(indexUnderlying=".STOXX50E").get()
test.exchangeC
class ImpliedVolatilityIPACalc():
def __init__( # Constroctor
self,
x: int = 15,
indexUnderlying: str = ".STOXX50E",
callOrPut: str = 'Put',
dateBack: int = 3,
expiryYearOfInterest: int = datetime.now().year,
riskFreeRate: Union[str, None] = None,
riskFreeRateField: Union[str, None] = None,
timeZoneInGraph: datetime = datetime.now().astimezone(),
maxColwidth: int = 200,
graphStyle: str = 'without out of trading hours', # 'with out of trading hours', '3 graphs', 'simple'
simpleGraphLineStyle: str = '.-', # 'o-'
simpleGraphSize: Tuple = (15, 5),
graphTemplate: str = 'plotly_dark',
debug: bool = False,
returnDfGraph: bool = False,
atOptionTradeOnly: bool = True):
self.x = x
self.indexUnderlying = indexUnderlying
self.callOrPut = callOrPut
self.dateBack = dateBack
self.expiryYearOfInterest = expiryYearOfInterest
self.riskFreeRate = riskFreeRate
self.riskFreeRateField = riskFreeRateField
self.timeZoneInGraph = timeZoneInGraph,
self.maxColwidth = maxColwidth
self.graphStyle = graphStyle
self.simpleGraphLineStyle = simpleGraphLineStyle
self.simpleGraphSize = simpleGraphSize
self.graphTemplate = graphTemplate
self.debug = debug
self.returnDfGraph = returnDfGraph
self.atOptionTradeOnly = atOptionTradeOnly
def Get_exp_dates(
self,
year: int = datetime.now().year,
days: bool = True,
mcal_get_calendar: str = 'EUREX') -> dict:
'''
Get_exp_dates Version 4.0:
This function gets expiration dates for a year for NDX options, which are the 3rd Fridays of each month.
Changes
----------------------------------------------
Changed from Version 1.0 to 2.0: Jonathan Legrand changed Haykaz Aramyan's original code to allow
(i) for the function's holiday argument to be changed, and defaulted to 'EUREX' as opposed to 'CBOE_Index_Options' and
(ii) for the function to output full date objects as opposed to just days of the month if agument days=True.
Changed from Version 2.0 to 3.0: Jonathan Legrand changed this function to reflec the fact that it can be used for indexes other than EUREX.
Changed from Version 3.0 to 4.0: Jonathan Legrand changed this function to be in line with PEP 3107 (type hints).
Dependencies
----------------------------------------------
Python library 'pandas_market_calendars' version 3.2
Parameters
-----------------------------------------------
Input:
year(int): year for which expiration days are requested
mcal_get_calendar(str): String of the calendar for which holidays have to be taken into account. More on this calendar (link to Github chacked 2022-10-11): https://github.com/rsheftel/pandas_market_calendars/blob/177e7922c7df5ad249b0d066b5c9e730a3ee8596/pandas_market_calendars/exchange_calendar_cboe.py
Default: mcal_get_calendar='EUREX'
days(bool): If True, only days of the month is outputed, else it's dataeime objects
Default: days=True
Output
-----------------------------------------------
dates(dict): dictionary of expiration days for each month of a specified year in datetime.date format.
'''
# get CBOE market holidays
Cal = mcal.get_calendar(mcal_get_calendar)
holidays = Cal.holidays().holidays
# set calendar starting from Saturday
c = calendar.Calendar(firstweekday=calendar.SATURDAY)
# get the 3rd Friday of each month
exp_dates = {}
for i in range(1, 13):
monthcal = c.monthdatescalendar(year, i)
date = monthcal[2][-1]
# check if found date is an holiday and get the previous date if it is
if date in holidays:
date = date + timedelta(-1)
# append the date to the dictionary
if year in exp_dates:
# # # Changed from original code from here on by Jonathan Legrand on 2022-10-11
if days: exp_dates[year].append(date.day)
else: exp_dates[year].append(date)
else:
if days: exp_dates[year] = [date.day]
else: exp_dates[year] = [date]
self.exp_dates = exp_dates
return exp_dates
def get(
self,
debug: bool = False) -> Tuple[str]:
if self.indexUnderlying == ".STOXX50E":
exchangeC, exchangeRIC, mcalGetCalendar = 'EUX', 'STX', 'EUREX'
elif self.indexUnderlying == '.SPX':
exchangeC, exchangeRIC, mcalGetCalendar = 'OPQ', 'SPX', 'CBOE_Futures' # 'CBOE_Index_Options' # should be 'CBOE_Index_Options'... CBOT_Equity
timeOfCalcDatetime = datetime.now() # For now, we will focuss on the use-case where we are calculating values for today; later we will allow for it historically for any day going back a few business days.
timeOfCalcStr = datetime.now().strftime('%Y-%m-%d')
fullDatesAtTimeOfCalc = self.Get_exp_dates(
year=timeOfCalcDatetime.year,
days=False,
mcal_get_calendar=mcalGetCalendar)
fullDatesAtTimeOfCalcDatetime = [
datetime(i.year, i.month, i.day)
for i in fullDatesAtTimeOfCalc[list(fullDatesAtTimeOfCalc.keys())[0]]]
expiryDateOfInt = [
i for i in fullDatesAtTimeOfCalcDatetime
if i > timeOfCalcDatetime + relativedelta(days=self.x)][0]
if self.debug: print(f"expiryDateOfInt: {expiryDateOfInt}")
return exchangeC, exchangeRIC, mcalGetCalendar
imp_vola_calc_ipa
exchangeC, exchangeRIC, mcalGetCalendar = index_underlying(underlyingName=".SPX")
def Get_exp_dates(year: int=datetime.now().year,
days: bool=True,
mcal_get_calendar: str=mcalGetCalendar
) -> dict:
'''
Get_exp_dates Version 4.0:
This function gets expiration dates for a year for NDX options, which are the 3rd Fridays of each month.
Changes
----------------------------------------------
Changed from Version 1.0 to 2.0: Jonathan Legrand changed Haykaz Aramyan's original code to allow
(i) for the function's holiday argument to be changed, and defaulted to 'EUREX' as opposed to 'CBOE_Index_Options' and
(ii) for the function to output full date objects as opposed to just days of the month if agument days=True.
Changed from Version 2.0 to 3.0: Jonathan Legrand changed this function to reflec the fact that it can be used for indexes other than EUREX.
Changed from Version 3.0 to 4.0: Jonathan Legrand changed this function to be in line with PEP 3107 (type hints).
Dependencies
----------------------------------------------
Python library 'pandas_market_calendars' version 3.2
Parameters
-----------------------------------------------
Input:
year(int): year for which expiration days are requested
mcal_get_calendar(str): String of the calendar for which holidays have to be taken into account. More on this calendar (link to Github chacked 2022-10-11): https://github.com/rsheftel/pandas_market_calendars/blob/177e7922c7df5ad249b0d066b5c9e730a3ee8596/pandas_market_calendars/exchange_calendar_cboe.py
Default: mcal_get_calendar='EUREX'
days(bool): If True, only days of the month is outputed, else it's dataeime objects
Default: days=True
Output:
dates(dict): dictionary of expiration days for each month of a specified year in datetime.date format.
'''
# get CBOE market holidays
Cal = mcal.get_calendar(mcal_get_calendar)
holidays = Cal.holidays().holidays
# set calendar starting from Saturday
c = calendar.Calendar(firstweekday=calendar.SATURDAY)
# get the 3rd Friday of each month
exp_dates = {}
for i in range(1, 13):
monthcal = c.monthdatescalendar(year, i)
date = monthcal[2][-1]
# check if found date is an holiday and get the previous date if it is
if date in holidays:
date = date + timedelta(-1)
# append the date to the dictionary
if year in exp_dates:
### Changed from original code from here on by Jonathan Legrand on 2022-10-11
if days: exp_dates[year].append(date.day)
else: exp_dates[year].append(date)
else:
if days: exp_dates[year] = [date.day]
else: exp_dates[year] = [date]
return exp_dates
As you can see, not only can we use IPA to gather large amounts of bespoke, calculated, values, but be can also portray this insight in a simple, quick and relevent way. The last cell in particular loops through our built fundction to give an updated graph every 5 seconds using 'legacy' technologies that would work in most environments (e.g.: Eikon Codebook).
Brilliant: Black-Scholes-Merton
What is the RIC syntax for options in Refinitiv Eikon?
Functions to find Option RICs traded on different exchanges